package org.hackystat.devcathlon.ui.page.profile.browse;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.hackystat.devcathlon.engine.profile.Profile;
import org.hackystat.devcathlon.engine.profile.ProfileManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;

/**
 * Profiles panel for listing all users within the system.
 * @author Anthony Du
 *
 */
public class ProfilesPanel extends Panel {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Profiles panel constructor.
   * @param id unique panel id
   */
  public ProfilesPanel(String id) {
    super(id);
    add(new ListView("profilesList", getProfiles()) {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(final ListItem item) {
        final Profile profile = (Profile) item.getModelObject();
        
        item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel() {
          private static final long serialVersionUID = 1L;

          public Object getObject() {
              return (item.getIndex() % 2 == 0) ? "odd" : "even";
          }
        }));
        
        Link profileLink = new Link("profileLink", item.getModel()) {
          /** Support serialization. */
          private static final long serialVersionUID = 1L;
          
          @Override
          public void onClick() {
            DevcathlonSession.get().getProfileSession().setProfile(profile.getUser().getEmail());
            setResponsePage(new ProfilePage());
          }
        };
        try {
          BufferedDynamicImageResource resource = new BufferedDynamicImageResource();     
          BufferedImage image;      
          image = ImageIO.read(new File(profile.getAvatar()));
          resource.setImage(image);
          profileLink.add(new Image("avatar", resource)); 
        }
        catch (IOException e) {
          profileLink.add(new Image("avatar"));
        }
        item.add(profileLink);
        item.add(new Label("screenName", profile.getUser().getUsername()));
        item.add(new Label("firstName", profile.getFirstName()));
        item.add(new Label("lastName", profile.getLastName()));
        item.add(new Label("email", profile.getUser().getEmail()));
      }
    });
  }
  
  /** 
   * Get the teams that are in the system.
   * @return The list of teams.
   */
  private List<?> getProfiles() {
    return ProfileManager.getInstance().getProfiles();
  }
}
